package com.nf.xiaomi.service.impl;


import com.nf.mvc.file.MultipartFile;
import com.nf.xiaomi.dao.CategoryDao;
import com.nf.xiaomi.dao.ProductDao;
import com.nf.xiaomi.dao.impl.CategoryDaoImpl;
import com.nf.xiaomi.dao.impl.ProductDaoImpl;
import com.nf.xiaomi.entity.ProductEntity;
import com.nf.xiaomi.service.ProductService;

import java.math.BigInteger;
import java.time.LocalDate;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    ProductDao productDao =new ProductDaoImpl();
    CategoryDao categoryDao = new CategoryDaoImpl();

    public List<ProductEntity> typeAll(String typeName) {
        int categoryID = categoryDao.selectCidByName(typeName);
        return productDao.getAll(categoryID);
    }

    public List<ProductEntity> getAll() {
        return productDao.getAll();
    }

    public List<ProductEntity> getAll(String name) {
        return productDao.getAll(name);
    }

    public Integer update(ProductEntity entity) {
        return productDao.update(entity);
    }

    public Integer deleteById(Integer id) {
        return productDao.deleteById(id);
    }

    @Override
    public BigInteger insert(ProductEntity entity, MultipartFile[] files) {
        String pictureList ="";
        entity.setState(1);
        for (int i = 0; i < files.length; i++) {
            String fileName = FileService.uploadFile("firstbuckets", files[i]);
            pictureList+=fileName;
            if(i != files.length-1){
                pictureList+=",";
            }
        }
        entity.setProductPictureList(pictureList);
        entity.setProductDate(LocalDate.now().atStartOfDay());
        return productDao.insert(entity);
    }

    @Override
    public Integer setProductAmount(Integer productID, Integer amount) {
        return productDao.setProductAmount(productID,amount);
    }
}
